package com.zj.osm;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.crashreport.CrashReport;
import com.zj.osm.entity.db.DaoMaster;
import com.zj.osm.entity.db.DaoSession;
import com.zj.osm.utils.NewDaoMaster;
import com.zj.osm.utils.SQLiteDatabaseUtils;
import com.zj.osm.view.BaseActivity;
import com.zj.utils.map.GoogleTileSource;
import com.zj.utils.swipe.SmartSwipeBack;
import com.zj.utils.utils.utilcode.SPUtils;

import java.util.LinkedList;

import static com.zj.osm.utils.Configuration.MAP_TILE_SOURCE;

/**
 * Application
 *
 * @author jiang zhu on 2019/10/21
 */
public class App extends Application {
    public boolean btn_model = true;
    public static final float FONT_SIZE = 1.0f; //字体大小倍数
    public static final String BHNE_DB = "bhnes.db";
    @SuppressLint("StaticFieldLeak")
    private static App instances;
    //非静态报空指针，坑死了
    private static DaoSession daoSession;
    public LinkedList<Activity> activityLinkedList = new LinkedList<>();
    private SmartSwipeBack.ActivitySwipeBackFilter activitySwipeBackFilter = activity -> (activity instanceof BaseActivity);
    public boolean isXinJian=true;

    public App() {
    }

    public static App getInstances() {
        if (instances == null) {
            synchronized (App.class) {
                if (instances == null) {
                    instances = new App();
                }
            }
        }
        return instances;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instances = this;
        initGreenDao();
        initData();
    }

    @Override
    public Resources getResources() {
        return super.getResources();
    }

    private void initData() {
        Bugly.init(getApplicationContext(), "50660d32b8", false);
        initDatabase();
        if (TextUtils.isEmpty(SPUtils.getInstance().getString(MAP_TILE_SOURCE))) {
            SPUtils.getInstance().put(MAP_TILE_SOURCE, GoogleTileSource.TILE_SOURCE_GOOGLE_HYBRID);
        }
        //在上一个方法的基础上，关联移动系数采用默认设置：0.5F
        //add relative moving slide back
        SmartSwipeBack.activitySlidingBack(this, activitySwipeBackFilter);
    }
    /**
     * 初始化GreenDao,直接在Application中进行初始化操作
     */
    private void initGreenDao() {
        //DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "bhne.db");
        NewDaoMaster helper = new NewDaoMaster(this, BHNE_DB, null); //升级数据库使用，数据不会丢失
        SQLiteDatabase db = helper.getWritableDatabase(); //不加密的写法
        DaoMaster daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }
    private void initDatabase() {
        String defaultMapFlag = SPUtils.getInstance().getString("DEFAULT_MAP_FLAG");
        if (defaultMapFlag.equals("")){
            SPUtils.getInstance().put("DEFAULT_MAP_FLAG","ONLINE_MAP");
        }
    }
    /**
     * 数据库相关
     * @return DaoSession
     */
    public DaoSession getDaoSession() {
        return daoSession;
    }


    public void addActivity(Activity activity) {
        activityLinkedList.add(activity);
    }

    public void removeActivity(Activity activity) {
        activityLinkedList.remove(activity);
    }

    public boolean IsLogin(){
        return "1".equals(com.blankj.utilcode.util.SPUtils.getInstance().getString("islogin"));
    }

    /**
     * 将所有类退出栈
     */
    public void exit(int permission) {
        if(permission>0) {
            SQLiteDatabaseUtils.getInstance().closeDatabase();
        }
        try {
            for (int i = 0; i < activityLinkedList.size(); i++) {
                activityLinkedList.get(i).finish();
            }
        } catch (Exception e) {
            System.exit(0);
            e.printStackTrace();
        }
    }
    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            layout.setPrimaryColorsId(R.color.refresh, R.color.black_66);//全局设置主题颜色
            return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            return new ClassicsFooter(context).setDrawableSize(20);
        });
    }

}
